package com.example.onlinemusicserver.controller;

import com.example.onlinemusicserver.model.User;
import com.example.onlinemusicserver.service.UserService;
import com.example.onlinemusicserver.tools.Constant;
import com.example.onlinemusicserver.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    /**
     * 用户登录
     * @param user
     * @param req
     * @return
     */
    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestBody User user, HttpServletRequest req) {
        User truUser = userService.selectByName(user.getUsername());
        if(truUser != null) {
            System.out.println("登陆成功");
            System.out.println(user.getPassword() + " " + truUser.getPassword());
            boolean flg = bCryptPasswordEncoder.matches(user.getPassword(),truUser.getPassword());
            if(!flg) {
                return new ResponseBodyMessage<>(-1,"当前账号密码错误!",user);
            }
            HttpSession session = req.getSession(true);
            session.setAttribute(Constant.USER_SESSION_KEY,truUser);
            return new ResponseBodyMessage<>(1,"登录成功!",truUser);
        }else{
            System.out.println("登录失败");
            return new ResponseBodyMessage<>(-1,"当前账号密码错误!",user);
        }
    }

    /**
     * 注册用户
     * @param user 用户信息
     * @return
     */
    @RequestMapping("/register")
    public ResponseBodyMessage<Boolean> register(@RequestBody User user) {
        User user1 = userService.selectByName(user.getUsername());
        if(user1 != null) {
            return new ResponseBodyMessage<>(-1,"当前用户已经存在",false);
        }else {
            User newUser = new User();
            newUser.setUsername(user.getUsername());
            String newPassword = bCryptPasswordEncoder.encode(user.getPassword());
            newUser.setPassword(newPassword);
            userService.addnewUser(newUser);
            return new ResponseBodyMessage<>(1,"注册成功",true);
        }
    }

    /**
     * 手机号快速登录
     * @param
     * @param
     * @throws IOException
     */
    @RequestMapping("code")
    public ResponseBodyMessage<Boolean> sendCode(@RequestParam("phone") String phone,HttpSession session){
return userService.sendCode(phone,session);
    }

    @RequestMapping("/logout")
    public void userLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);
        // 拦截器的拦截, 所以不可能出现session为空的情况
        session.removeAttribute(Constant.USER_SESSION_KEY);
        response.sendRedirect("login.html");
    }
}
